  <template>
  <div class="cart_page">
    <table>
      <thead>
        <tr>
          <th style="width: 8%">
            <i
              @click="clickCheckAll"
              :class="
                  checkAll
                    ? 'iconfont icon-yduifuxuankuangxuanzhong'
                    : 'iconfont icon-yduifuxuankuang'
                "
            ></i>
          </th>
          <th style="width: 30%">礼品信息</th>
          <th>兑换分数</th>
          <th>数量</th>
          <th>小计 (鸡腿)</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in cartList" :key="item.id">
          <td>
            <i
              @click="theRadioClick(index, item.id)"
              :class="
                  checkList[index]
                    ? 'iconfont icon-yduifuxuankuangxuanzhong'
                    : 'iconfont icon-yduifuxuankuang'
                "
            ></i>
          </td>
          <td>
            <section>
              <img width="84" :src="imgbaseurl + item.coverImg" alt="列表图片" />
              <div class="info">
                <h5>{{ item.title }}</h5>
                <p>{{ item.versionDescription }}</p>
              </div>
            </section>
          </td>
          <td>{{ item.coin }}鸡腿</td>
          <td>
            <div class="step">
              <span @click="stepClick(-1, index)">-</span>
              <input type="text" disabled v-model="cartList[index].total" />
              <span @click="stepClick(1, index)">+</span>
            </div>
          </td>
          <td>{{ item.coin * item.total }}鸡腿</td>
          <td>
            <span @click="deleteTheGoods(item.id, index)" class="del">删除</span>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="total">
      总计：
      <span>{{ totalCoin }}鸡腿</span>
    </div>
    <div class="submit">提交</div>
    <div @click="deleteTheSelectedFn" class="deleteTheSelected">删除选中</div>
  </div>
</template>
  <script>
import { ShowCartAPI, deleteTheGoodsAPI } from "@/request/api";
export default {
  data() {
    return {
      stepNum: 1,
      // 全选
      checkAllStatus: false,
      // 单选按钮数组
      checkList: [],
      //购物车数据
      cartList: [],
      //需要批量删除的id
      batchDeleteId: []
    };
  },
  methods: {
    //批量删除（删除选中商品）
    deleteTheSelectedFn() {
      this.cartList = this.cartList.filter((e, i) => {
        return this.checkList[i] == false;
      });
      this.checkList = this.checkList.filter(e => {
        return !e;
      });
      // 从服务器也删除
      this.batchDeleteId.forEach(e => {
        this.deleteCartAPI(e);
      });
      this.$store.dispatch("showtoast/aysncchanisshowtoast", {
        msg: "批量删除成功",
        type: "success"
      });
      //更新topbar
      this.$emit("updateKey");
    },
    //删除商品函数
    deleteTheGoods(id, index) {
      this.deleteCartAPI(id);
      this.cartList.splice(index, 1);
      this.checkList.splice(index, 1);
      this.$store.dispatch("showtoast/aysncchanisshowtoast", {
        msg: "删除成功",
        type: "success"
      });
      //更新topbar
      this.$emit("updateKey");
    },
    //删除商品请求接口
    async deleteCartAPI(id) {
      let res = await deleteTheGoodsAPI(id);
      if (!res) return;
    },
    //加减按钮计步器函数
    stepClick(val, index) {
      if (val == -1 && this.cartList[index].total <= 1) return;
      this.cartList[index].total += val;
    },
    clickCheckAll() {
      this.checkAll = !this.checkAll;
    },
    //单选
    theRadioClick(index, id) {
      //如果需要删除的id数组里面已经有，就将其删除，没有就添加
      if (this.batchDeleteId.includes(id)) {
        this.batchDeleteId.splice(this.batchDeleteId.indexOf(id), 1);
      } else {
        this.batchDeleteId.push(id);
      }
      this.$set(this.checkList, index, !this.checkList[index]);
    }
  },
  async created() {
    let res = await ShowCartAPI();
    if (!res) return;

    this.cartList = res.data;
    // 渲染单选状态列表
    this.cartList.map((e, i) => {
      this.checkList.push(false);
    });
  },
  computed: {
    //计算总鸡腿数
    totalCoin() {
      let index = -1;
      return this.cartList.reduce((accumulator, current) => {
        ++index;
        //  console.log("执行了", this.checkList[index]);
        return (accumulator += this.checkList[index]
          ? current.total * current.coin
          : 0);
      }, 0);
    },
    checkAll: {
      get() {
        // return this.checkList.length ? !this.checkList.includes(false) : false;
        return this.checkList.length && !this.checkList.includes(false);
      },
      set(val) {
        this.checkList.forEach((e, i) => {
          this.$set(this.checkList, i, val);
        });
      }
    }
  }
};
</script>
  <style lang="scss" scoped>
.cart_page {
  background: #fff;
  table {
    width: 100%;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
    color: #666;
    border-collapse: collapse;
    font-size: 14px;
    thead {
      background: #f2f2f2;
      th {
        padding: 19px 0;
        .iconfont {
          cursor: pointer;
        }
        .icon-yduifuxuankuangxuanzhong {
          color: #0a328e;
        }
      }
    }
    tbody {
      tr {
        td {
          vertical-align: middle;
          text-align: center;
          padding: 19px 0;
          table-layout: fixed; // td的宽度固定，不随内容变化
          .iconfont {
            cursor: pointer;
          }
          .icon-yduifuxuankuangxuanzhong {
            color: #0a328e;
          }
          section {
            padding-left: 20px;
            display: flex;
            box-sizing: border-box;
            img {
              margin-right: 12px;
            }
            .info {
              padding-top: 20px;
              flex: 1;
              overflow: hidden;
              box-sizing: border-box;
              text-align: left;
              h5 {
                overflow: hidden;
                color: #333;
                font-size: 18px;
                white-space: nowrap;
                text-overflow: ellipsis;
                margin-bottom: 20px;
              }
              p {
                color: #666;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              }
            }
          }
          .step {
            width: 106px;
            height: 32px;
            margin: auto;
            span {
              float: left;
              width: 30px;
              height: 32px;
              display: block;
              border: solid 1px #d1d1d1;
              font-size: 20px;
              box-sizing: border-box;
              font-weight: normal;
              font-stretch: normal;
              line-height: 30px;
              letter-spacing: 0px;
              color: #999999;
              text-align: center;
              cursor: pointer;
              background: #fff;
            }
            input {
              box-sizing: border-box;
              width: 46px;
              height: 32px;
              float: left;
              text-align: center;
              font-size: 14px;
              line-height: 23px;
              letter-spacing: 0px;
              color: #666666;
              border: 0;
              border-top: 1px solid #d1d1d1;
              border-bottom: 1px solid #d1d1d1;
              background: #fff;
            }
          }
          .del {
            border: 1px solid #ececec;
            border-radius: 4px;
            padding: 5px 10px;
            cursor: pointer;
            &:hover {
              color: #fff;
              background: #0a328e;
            }
          }
        }
      }
    }
  }
  .total {
    padding: 30px 0;
    text-align: right;
    font-size: 22px;
    span {
      font-weight: bold;
      color: #fd604d;
    }
  }
  .submit {
    width: 175px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-family: SourceHanSansSC-Light;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0px;
    color: #ffffff;
    cursor: pointer;
    background-color: #0a328e;
    float: right;
  }
  .deleteTheSelected {
    width: 175px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-family: SourceHanSansSC-Light;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0px;
    color: #ffffff;
    cursor: pointer;
    background-color: #8e110a;
    float: right;
    margin-right: 10px;
  }
}
</style>